home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / widgets / Label.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-01  |  14.6 KB  |  490 lines

  1. #ifndef lint
  2. static char Xrcsid[] = "$XConsortium: Label.c,v 1.77 89/12/08 12:35:36 swick Exp $";
  3. #endif /* lint */
  4.  
  5.  
  6. /***********************************************************
  7. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  8. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  9.  
  10.                         All Rights Reserved
  11.  
  12. Permission to use, copy, modify, and distribute this software and its 
  13. documentation for any purpose and without fee is hereby granted, 
  14. provided that the above copyright notice appear in all copies and that
  15. both that copyright notice and this permission notice appear in 
  16. supporting documentation, and that the names of Digital or MIT not be
  17. used in advertising or publicity pertaining to distribution of the
  18. software without specific, written prior permission.  
  19.  
  20. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  21. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  22. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  23. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  25. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  26. SOFTWARE.
  27.  
  28. ******************************************************************/
  29.  
  30. /*
  31.  * Label.c - Label widget
  32.  * Adapted 9/1/90 M.Seutter & J. van Krieken
  33.  */
  34.  
  35. #define XtStrlen(s)        ((s) ? strlen(s) : 0)
  36.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <X11/IntrinsicP.h>
  40. #include <X11/StringDefs.h>
  41. #include <X11/Xaw/XawInit.h>
  42. #include "LabelP.h"
  43.  
  44. #define streq(a,b) (strcmp( (a), (b) ) == 0)
  45. #define MULTI_LINE_LABEL 32767
  46.  
  47. /****************************************************************
  48.  *
  49.  * Full class record constant
  50.  *
  51.  ****************************************************************/
  52.  
  53. /* Private Data */
  54.  
  55. #define offset(field) XtOffset(LabelWidget, field)
  56. static XtResource resources[] = {
  57.     {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  58.     offset(label.foreground), XtRString, "XtDefaultForeground"},
  59.     {XtNfont,  XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  60.     offset(label.font),XtRString, "XtDefaultFont"},
  61.     {XtNlabel,  XtCLabel, XtRString, sizeof(String),
  62.     offset(label.label), XtRString, NULL},
  63.     {XtNimageText, XtCMode, XtRBoolean, sizeof(Boolean),
  64.     offset(label.image_text), XtRBoolean, (caddr_t)False},
  65.     {XtNjustify, XtCJustify, XtRJustify, sizeof(XtJustify),
  66.     offset(label.justify), XtRImmediate, (caddr_t)XtJustifyCenter},
  67.     {XtNinternalWidth, XtCWidth, XtRDimension,  sizeof(Dimension),
  68.     offset(label.internal_width), XtRImmediate, (caddr_t)4},
  69.     {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
  70.     offset(label.internal_height), XtRImmediate, (caddr_t)2},
  71.     {XtNbitmap, XtCPixmap, XtRBitmap, sizeof(Pixmap),
  72.     offset(label.pixmap), XtRImmediate, (caddr_t)None},
  73.     {XtNresize, XtCResize, XtRBoolean, sizeof(Boolean),
  74.     offset(label.resize), XtRImmediate, (caddr_t)True},
  75. };
  76.  
  77. static void Initialize();
  78. static void Resize();
  79. static void Redisplay();
  80. static Boolean SetValues();
  81. static void ClassInitialize();
  82. static void Destroy();
  83. static XtGeometryResult QueryGeometry();
  84.  
  85. LabelClassRec labelClassRec = {
  86.   {
  87. /* core_class fields */    
  88. #define superclass        (&simpleClassRec)
  89.     /* superclass          */    (WidgetClass) superclass,
  90.     /* class_name          */    "Label",
  91.     /* widget_size          */    sizeof(LabelRec),
  92.     /* class_initialize       */    ClassInitialize,
  93.     /* class_part_initialize    */    NULL,
  94.     /* class_inited           */    FALSE,
  95.     /* initialize          */    Initialize,
  96.     /* initialize_hook        */    NULL,
  97.     /* realize              */    XtInheritRealize,
  98.     /* actions              */    NULL,
  99.     /* num_actions          */    0,
  100.     /* resources          */    resources,
  101.     /* num_resources          */    XtNumber(resources),
  102.     /* xrm_class          */    NULLQUARK,
  103.     /* compress_motion          */    TRUE,
  104.     /* compress_exposure      */    TRUE,
  105.     /* compress_enterleave    */    TRUE,
  106.     /* visible_interest          */    FALSE,
  107.     /* destroy              */    Destroy,
  108.     /* resize              */    Resize,
  109.     /* expose              */    Redisplay,
  110.     /* set_values          */    SetValues,
  111.     /* set_values_hook        */    NULL,
  112.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  113.     /* get_values_hook        */    NULL,
  114.     /* accept_focus         */    NULL,
  115.     /* version            */    XtVersion,
  116.     /* callback_private       */    NULL,
  117.     /* tm_table               */    NULL,
  118.     /* query_geometry        */    QueryGeometry,
  119.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  120.     /* extension        */    NULL
  121.   },
  122. /* Simple class fields initialization */
  123.   {
  124.     /* change_sensitive        */    XtInheritChangeSensitive
  125.   }
  126. };
  127. WidgetClass labelWidgetClass = (WidgetClass)&labelClassRec;
  128. /****************************************************************
  129.  *
  130.  * Private Procedures
  131.  *
  132.  ****************************************************************/
  133.  
  134. static void ClassInitialize()
  135. {
  136.     extern void XmuCvtStringToJustify();
  137.     extern void XmuCvtStringToBitmap();
  138.     static XtConvertArgRec screenConvertArg[] = {
  139.         {XtWidgetBaseOffset, (caddr_t) XtOffset(Widget, core.screen),
  140.          sizeof(Screen *)}
  141.     };
  142.     XawInitializeWidgetSet();
  143.     XtAddConverter( XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0 );
  144.     XtAddConverter("String", "Bitmap", XmuCvtStringToBitmap,
  145.            screenConvertArg, XtNumber(screenConvertArg));
  146. } /* ClassInitialize */
  147.  
  148. /*
  149.  * Calculate width and height of displayed text in pixels
  150.  */
  151.  
  152. static void SetTextWidthAndHeight(lw)
  153.     LabelWidget lw;
  154. {
  155.     register XFontStruct    *fs = lw->label.font;
  156.     char *nl;
  157.  
  158.     if (lw->label.pixmap != None) {
  159.     Window root;
  160.     int x, y;
  161.     unsigned int width, height, bw, depth;
  162.     if (XGetGeometry(XtDisplay(lw), lw->label.pixmap, &root, &x, &y,
  163.              &width, &height, &bw, &depth)) {
  164.         lw->label.label_height = height;
  165.         lw->label.label_width = width;
  166.         lw->label.label_len = depth;
  167.         return;
  168.     }
  169.     }
  170.  
  171.     lw->label.label_height = fs->max_bounds.ascent + fs->max_bounds.descent;
  172.     if (lw->label.label == NULL) {
  173.     lw->label.label_len = 0;
  174.     lw->label.label_width = 0;
  175.     }
  176.     else if ((nl = index(lw->label.label, '\n')) != NULL) {
  177.     char *label;
  178.     lw->label.label_len = MULTI_LINE_LABEL;
  179.     lw->label.label_width = 0;
  180.     for (label = lw->label.label; nl != NULL; nl = index(label, '\n')) {
  181.         int width = XTextWidth(fs, label, (int)(nl - label));
  182.         if (width > lw->label.label_width) lw->label.label_width = width;
  183.         label = nl + 1;
  184.         if (*label)
  185.         lw->label.label_height +=
  186.             fs->max_bounds.ascent + fs->max_bounds.descent;
  187.     }
  188.     if (*label) {
  189.         int width = XTextWidth(fs, label, strlen(label));
  190.         if (width > lw->label.label_width) lw->label.label_width = width;
  191.     }
  192.     } else {
  193.     lw->label.label_len = strlen(lw->label.label);
  194.     lw->label.label_width =
  195.         XTextWidth(fs, lw->label.label, (int) lw->label.label_len);
  196.     }
  197. }
  198.  
  199. static void GetnormalGC(lw)
  200.     LabelWidget lw;
  201. {
  202.     XGCValues    values;
  203.  
  204.     values.foreground    = lw->label.foreground;
  205.     values.background    = lw->core.background_pixel;
  206.     values.font        = lw->label.font->fid;
  207.  
  208.     lw->label.normal_GC = XtGetGC(
  209.     (Widget)lw,
  210.     (unsigned) GCForeground | GCBackground | GCFont,
  211.     &values);
  212. }
  213.  
  214. static void GetgrayGC(lw)
  215.     LabelWidget lw;
  216. {
  217.     XGCValues    values;
  218.  
  219.     values.foreground = lw->label.foreground;
  220.     values.background = lw->core.background_pixel;
  221.     values.font          = lw->label.font->fid;
  222.     values.fill_style = FillTiled;
  223.     values.tile       = XmuCreateStippledPixmap(XtScreen((Widget)lw),
  224.                         lw->label.foreground, 
  225.                         lw->core.background_pixel,
  226.                         lw->core.depth);
  227.  
  228.     lw->label.stipple = values.tile;
  229.     lw->label.gray_GC = XtGetGC((Widget)lw, 
  230.                 (unsigned) GCForeground | GCBackground |
  231.                        GCFont | GCTile | GCFillStyle,
  232.                 &values);
  233. }
  234.  
  235. /* ARGSUSED */
  236. static void Initialize(request, new)
  237.  Widget request, new;
  238. {
  239.     LabelWidget lw = (LabelWidget) new;
  240.  
  241.     if (lw->label.label == NULL) 
  242.         lw->label.label = XtNewString(lw->core.name);
  243.     else {
  244.         lw->label.label = XtNewString(lw->label.label);
  245.     }
  246.  
  247.     GetnormalGC(lw);
  248.     GetgrayGC(lw);
  249.  
  250.     SetTextWidthAndHeight(lw);
  251.  
  252.     if (lw->core.width == 0)
  253.         lw->core.width = lw->label.label_width + 2 * lw->label.internal_width;
  254.     if (lw->core.height == 0)
  255.         lw->core.height = lw->label.label_height + 2*lw->label.internal_height;
  256.  
  257.     lw->label.label_x = lw->label.label_y = 0;
  258.     (*XtClass(new)->core_class.resize) ((Widget)lw);
  259.  
  260. } /* Initialize */
  261.  
  262. /*
  263.  * Repaint the widget window
  264.  */
  265.  
  266. /* ARGSUSED */
  267. static void Redisplay(w, event, region)
  268.     Widget w;
  269.     XEvent *event;
  270.     Region region;
  271. {
  272.    LabelWidget lw = (LabelWidget) w;
  273.    GC gc;
  274.  
  275.    if (region != NULL &&
  276.        XRectInRegion(region, lw->label.label_x, lw->label.label_y,
  277.              lw->label.label_width, lw->label.label_height)
  278.          == RectangleOut)
  279.        return;
  280.  
  281.    gc = XtIsSensitive(lw) ? lw->label.normal_GC : lw->label.gray_GC;
  282. #ifdef notdef
  283.    if (region != NULL) XSetRegion(XtDisplay(w), gc, region);
  284. #endif /*notdef*/
  285.    if (lw->label.pixmap == None) {
  286.        int len = lw->label.label_len;
  287.        char *label = lw->label.label;
  288.        Position y = lw->label.label_y + lw->label.font->max_bounds.ascent;
  289.        if (len == MULTI_LINE_LABEL) {
  290.        char *nl;
  291.        while ((nl = index(label, '\n')) != NULL) {
  292.            XDrawString(
  293.                XtDisplay(w), XtWindow(w), gc, lw->label.label_x,
  294.                y, label, (int)(nl - label));
  295.            y += lw->label.font->max_bounds.ascent + lw->label.font->max_bounds.descent;
  296.            label = nl + 1;
  297.        }
  298.        len = strlen(label);
  299.        }
  300.        if (len)
  301.        { if (lw->label.image_text)
  302.             { XDrawImageString(
  303.             XtDisplay(w), XtWindow(w), gc, lw->label.label_x,
  304.             y, label, len);
  305.             }
  306.          else
  307.                 { XDrawString(
  308.             XtDisplay(w), XtWindow(w), gc, lw->label.label_x,
  309.             y, label, len);
  310.             };
  311.        }
  312.    } else if (lw->label.label_len == 1) { /* depth */
  313.        XCopyPlane(
  314.           XtDisplay(w), lw->label.pixmap, XtWindow(w), gc,
  315.           0, 0, lw->label.label_width, lw->label.label_height,
  316.           lw->label.label_x, lw->label.label_y, 1L);
  317.    } else {
  318.        XCopyArea(
  319.          XtDisplay(w), lw->label.pixmap, XtWindow(w), gc,
  320.          0, 0, lw->label.label_width, lw->label.label_height,
  321.          lw->label.label_x, lw->label.label_y);
  322.    }
  323. #ifdef notdef
  324.    if (region != NULL) XSetClipMask(XtDisplay(w), gc, (Pixmap)None);
  325. #endif /* notdef */
  326. }
  327.  
  328. static void _Reposition(lw, width, height, dx, dy)
  329.     register LabelWidget lw;
  330.     Dimension width, height;
  331.     Position *dx, *dy;
  332. {
  333.     Position newPos;
  334.     switch (lw->label.justify) {
  335.  
  336.     case XtJustifyLeft   :
  337.         newPos = lw->label.internal_width;
  338.         break;
  339.  
  340.     case XtJustifyRight  :
  341.         newPos = width -
  342.         (lw->label.label_width + lw->label.internal_width);
  343.         break;
  344.  
  345.     case XtJustifyCenter :
  346.         newPos = (width - lw->label.label_width) / 2;
  347.         break;
  348.     }
  349.     if (newPos < (Position)lw->label.internal_width)
  350.     newPos = lw->label.internal_width;
  351.     *dx = newPos - lw->label.label_x;
  352.     lw->label.label_x = newPos;
  353.     *dy = (newPos = (height - lw->label.label_height) / 2) - lw->label.label_y;
  354.     lw->label.label_y = newPos;
  355.     return;
  356. }
  357.  
  358. static void Resize(w)
  359.     Widget w;
  360. {
  361.     LabelWidget lw = (LabelWidget)w;
  362.     Position dx, dy;
  363.     _Reposition(lw, w->core.width, w->core.height, &dx, &dy);
  364. }
  365.  
  366. /*
  367.  * Set specified arguments into widget
  368.  */
  369.  
  370. #define PIXMAP 0
  371. #define WIDTH 1
  372. #define HEIGHT 2
  373. #define NUM_CHECKS 3
  374.  
  375. static Boolean SetValues(current, request, new, args, num_args)
  376.     Widget current, request, new;
  377.     ArgList args;
  378.     Cardinal *num_args;
  379. {
  380.     LabelWidget curlw = (LabelWidget) current;
  381.     LabelWidget reqlw = (LabelWidget) request;
  382.     LabelWidget newlw = (LabelWidget) new;
  383.     int i;
  384.     Boolean was_resized = False, redisplay = False, checks[NUM_CHECKS];
  385.  
  386.     for (i = 0; i < NUM_CHECKS; i++)
  387.     checks[i] = FALSE;
  388.  
  389.     for (i = 0; i < *num_args; i++) {
  390.     if (streq(XtNbitmap, args[i].name))
  391.         checks[PIXMAP] = TRUE;
  392.     if (streq(XtNwidth, args[i].name))
  393.         checks[WIDTH] = TRUE;
  394.     if (streq(XtNheight, args[i].name))
  395.         checks[HEIGHT] = TRUE;
  396.     }
  397.  
  398.     if (newlw->label.label == NULL) {
  399.     newlw->label.label = newlw->core.name;
  400.     }
  401.  
  402.     if (curlw->label.label != newlw->label.label) {
  403.         if (curlw->label.label != curlw->core.name)
  404.         XtFree( (char *)curlw->label.label );
  405.  
  406.     if (newlw->label.label != newlw->core.name) {
  407.         newlw->label.label = XtNewString( newlw->label.label );
  408.     }
  409.     was_resized = True;
  410.     }
  411.  
  412.     if (was_resized || (curlw->label.font != newlw->label.font) ||
  413.     (curlw->label.justify != newlw->label.justify) || checks[PIXMAP]) {
  414.  
  415.     SetTextWidthAndHeight(newlw);
  416.     was_resized = True;
  417.     }
  418.  
  419.     /* recalculate the window size if something has changed. */
  420.     if (newlw->label.resize && was_resized) {
  421.     if ((curlw->core.width == reqlw->core.width) && !checks[WIDTH])
  422.         newlw->core.width = (newlw->label.label_width +
  423.                  2 * newlw->label.internal_width);
  424.  
  425.     if ((curlw->core.height == reqlw->core.height) && !checks[HEIGHT])
  426.         newlw->core.height = (newlw->label.label_height + 
  427.                   2 * newlw->label.internal_height);
  428.     }
  429.  
  430.     if (curlw->label.foreground != newlw->label.foreground
  431.     || curlw->label.font->fid != newlw->label.font->fid) {
  432.  
  433.     XtReleaseGC(new, curlw->label.normal_GC);
  434.     XtReleaseGC(new, curlw->label.gray_GC);
  435.     XmuReleaseStippledPixmap( XtScreen(current), curlw->label.stipple );
  436.     GetnormalGC(newlw);
  437.     GetgrayGC(newlw);
  438.     redisplay = True;
  439.     }
  440.  
  441.     if ((curlw->label.internal_width != newlw->label.internal_width)
  442.         || (curlw->label.internal_height != newlw->label.internal_height)
  443.     || was_resized) {
  444.     /* Resize() will be called if geometry changes succeed */
  445.     Position dx, dy;
  446.     _Reposition(newlw, curlw->core.width, curlw->core.height, &dx, &dy);
  447.     }
  448.  
  449.     /* This is a HACK...... */
  450.     if (!newlw->label.resize && was_resized && newlw->core.visible)
  451.        { XClearWindow (XtDisplay (newlw), XtWindow (newlw));
  452.          Redisplay(newlw,NULL,NULL);
  453.          return False; }
  454.     else return was_resized || redisplay ||
  455.        XtIsSensitive(current) != XtIsSensitive(new);
  456. }
  457.  
  458. static void Destroy(w)
  459.     Widget w;
  460. {
  461.     LabelWidget lw = (LabelWidget)w;
  462.  
  463.     XtFree( lw->label.label );
  464.     XtReleaseGC( w, lw->label.normal_GC );
  465.     XtReleaseGC( w, lw->label.gray_GC);
  466.     XmuReleaseStippledPixmap( XtScreen(w), lw->label.stipple );
  467. }
  468.  
  469.  
  470. static XtGeometryResult QueryGeometry(w, intended, preferred)
  471.     Widget w;
  472.     XtWidgetGeometry *intended, *preferred;
  473. {
  474.     register LabelWidget lw = (LabelWidget)w;
  475.  
  476.     preferred->request_mode = CWWidth | CWHeight;
  477.     preferred->width = lw->label.label_width + 2 * lw->label.internal_width;
  478.     preferred->height = lw->label.label_height + 2*lw->label.internal_height;
  479.     if (  ((intended->request_mode & (CWWidth | CWHeight))
  480.            == (CWWidth | CWHeight)) &&
  481.       intended->width == preferred->width &&
  482.       intended->height == preferred->height)
  483.     return XtGeometryYes;
  484.     else if (preferred->width == w->core.width &&
  485.          preferred->height == w->core.height)
  486.     return XtGeometryNo;
  487.     else
  488.     return XtGeometryAlmost;
  489. }
  490.